/* Project vm
Volt meter PIC16F877
*/

#include <pic.h>

__CONFIG(
PWRTEN
& BORDIS
& UNPROTECT
& WDTDIS
& LVPDIS
& HS
);

__IDLOC(F877);

#define E RB6

void lcd_w_com4(unsigned char d){
PORTB = d & 0x0f;
E = 1; NOP();
E = 0;
}

void lcd_w_chr4(unsigned char d){
PORTB = (d & 0x0f) | 0x10;
E = 1; NOP();
E = 0;
}

void lcd_bfck(void){
unsigned char d;

PORTB = 0;
TRISB3 = 1;
PORTB = 0b00100000;

do{
E = 1; NOP();
d = RB3;
E = 0;
NOP(); NOP();
E = 1; NOP();
E = 0;
NOP(); NOP();
} while(d);

PORTB = 0;
TRISB3 = 0;
}

void lcd_putcom(unsigned char d){
lcd_bfck();
lcd_w_com4(d >> 4);
lcd_w_com4(d);
}

void lcd_locate(
unsigned char x, unsigned char y){
lcd_putcom((x + 0x40 * y) | 0x80);
}

void lcd_putchr(unsigned char d){
lcd_bfck();
lcd_w_chr4(d >> 4);
lcd_w_chr4(d);
}

void lcd_puts(const unsigned char *s){
while(*s) lcd_putchr(*s++);
}

void lcd_putv(unsigned int v){

unsigned char i;
unsigned char buf[5];

v *= 49;

for(i = 0; i < 5; i++)
buf[i] = '0';
i = 4;
do {
buf[i] = (v % 10) + '0';
v = v / 10;
i--;
} while(v > 0);

lcd_putchr(buf[0]);
lcd_putchr('.');
for(i = 1; i < 4; i++)
lcd_putchr(buf[i]);
lcd_puts("V");
}

void main(void){

unsigned int v;
unsigned int v0, v1;

OPTION = 0b10000111;
PORTB = 0;
TRISB = 0b10000000;
ADCON1 = 0b10000100;
ADCON0 = 0b10000001;
PORTA = 0;
TRISA = 0b00001011;

TMR0 = 0; while(TMR0 < 255);
lcd_w_com4(0x03);
TMR0 = 0; while(TMR0 < 80);
lcd_w_com4(0x03);
TMR0 = 0; while(TMR0 < 2);
lcd_w_com4(0x03);
TMR0 = 0; while(TMR0 < 2);
lcd_w_com4(0x02);

lcd_putcom(0x28);
lcd_putcom(0x01);
lcd_putcom(0x0c);

lcd_puts("CH0 CH1");

while(1){

//Channel 0
ADCON0 = 0b10000001;
TMR0 = 0; while(TMR0 < 1);
GODONE = 1;
while(GODONE);

v = (ADRESH * 256) + ADRESL;
if(v != v0){
v0 = v;
lcd_locate(0, 1);
lcd_putv(v0);
}

//Cannel 1
ADCON0 = 0b10001001;
TMR0 = 0; while(TMR0 < 1);
GODONE = 1;
while(GODONE);

v = (ADRESH * 256) + ADRESL;
if(v != v1){
v1 = v;
lcd_locate(7, 1);
lcd_putv(v1);
}
}
}


                 戻る